package com.changyue.ui;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;

import com.changyue.temp.R;


public class HeadView extends FrameLayout {

    private TextView mTitle;
    private ImageView mLeft;
    private ImageView mLeftLoading;
    private TextView mRightTxt;
    private OnRightClickListener mOnRightClickListener;
    private OnLeftClickListener mOnLeftClickListener;
    private Activity mActivity;

    public HeadView(Context context) {
        this(context, null);
    }

    public HeadView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater.from(context).inflate(
                R.layout.head_view, this);

        TypedArray ta = context.getTheme()
                .obtainStyledAttributes(attrs, R.styleable.HeadView, defStyleAttr, R.style.HeadViewDefStyle);

        mTitle = (TextView) findViewById(R.id.tx_head_title);
        mLeft = (ImageView) findViewById(R.id.img_head_left);
        mLeftLoading = (ImageView) findViewById(R.id.img_head_left_loading);
        mRightTxt = (TextView) findViewById(R.id.tx_head_right);

        String title = ta.getString(R.styleable.HeadView_head_title);
        mTitle.setText(title);

        int color = ta.getColor(R.styleable.HeadView_text_color, ContextCompat.getColor(context, R.color.white));
        mTitle.setTextColor(color);
        mRightTxt.setTextColor(color);

        int leftImg = ta.getResourceId(R.styleable.HeadView_left_image, 0);
        if (leftImg != 0) {
            mLeft.setImageResource(leftImg);
            mLeft.setVisibility(VISIBLE);
        }

        String rightTxt = ta.getString(R.styleable.HeadView_right_text);
        if (!TextUtils.isEmpty(rightTxt)) {
            mRightTxt.setText(rightTxt);
            mRightTxt.setVisibility(VISIBLE);
        }

        setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));
        // 默认结束 Activity
        mLeft.setOnClickListener(view -> {
            if (null != mOnLeftClickListener) {
                mOnLeftClickListener.onLeftClickListener(view);
            } else {
                ((Activity) context).finish();
            }
        });

        mLeftLoading.setOnClickListener(v -> {
            if (null != mOnLeftClickListener) {
                mOnLeftClickListener.onLeftClickListener(v);
            }
        });

        OnClickListener rightListener = v -> {
            if (null != mOnRightClickListener) {
                mOnRightClickListener.onRightClickListener(v);
            }
        };
        mRightTxt.setOnClickListener(rightListener);

        ta.recycle();
    }

    public void setTitle(String title) {
        mTitle.setText(title);
    }

    public String getTitle() {
        return mTitle.getText().toString();
    }

    public void setTitle(@StringRes int title) {
        mTitle.setText(title);
    }

    public void setLeftVisibility(boolean visible) {
        mLeft.setVisibility(visible ? VISIBLE : GONE);
    }

    public void setLeftLoadingVisibility(boolean visible) {
        mLeftLoading.setVisibility(visible ? VISIBLE : GONE);
    }

    public void setRightTxtVisibility(boolean visible) {
        mRightTxt.setVisibility(visible ? VISIBLE : GONE);
    }

    public void setRightTxt(String txt) {
        mRightTxt.setText(txt);
    }

    public void setRightTxt(@StringRes int txt) {
        mRightTxt.setText(txt);
    }

    public View getLeftView() {
        return mLeft;
    }

    public View getRightTextView() {
        return mRightTxt;
    }

    public void setOnRightClickListener(OnRightClickListener onRightClickListener) {
        mOnRightClickListener = onRightClickListener;
    }

    public void setOnLeftClickListener(OnLeftClickListener onLeftClickListener) {
        mOnLeftClickListener = onLeftClickListener;
    }

    public interface OnRightClickListener {
        void onRightClickListener(View view);
    }

    public interface OnLeftClickListener {
        void onLeftClickListener(View view);
    }

    public Activity getActivity() {
        return mActivity;
    }

    public void setActivity(Activity activity) {
        mActivity = activity;
    }
}
